package com.example.seakdreamandroid.Asmr;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.Toast;

import com.example.seakdreamandroid.R;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class AsmrActivity extends AppCompatActivity {

    private List<Fragment> mFragments; //存放视图
    private ViewPager viewPager;
    private TabLayout mTabLayout;
    private List<String> mtitle; //存放底部标题



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_asmr);

        initView();
    }
    private void initView() {
        mTabLayout = findViewById(R.id.tablayout_asmr);
        viewPager = findViewById(R.id.viewpager_asmr);


        mFragments = new ArrayList<>();
        mFragments.add(new SelectedAsmrFragment());
        mFragments.add(new ImmerseAsmrFragment());
        mFragments.add(new SelectedAsmrFragment());
        mFragments.add(new RecommendAsmrFragment());

        mtitle = new ArrayList<String>();
        mtitle.add("精选");
        mtitle.add("沉浸时刻");
        mtitle.add("自由创作");
        mtitle.add("官方推荐");

        //实例化适配器
        AsmrAdapter asmrAdapter = new AsmrAdapter(getSupportFragmentManager(),mFragments,mtitle);
        viewPager.setAdapter(asmrAdapter);

        mTabLayout.setupWithViewPager(viewPager);//给tab设置一个viewpager
        //viewpager的监听
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override //选中
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        Toast.makeText(AsmrActivity.this,"精选界面",Toast.LENGTH_SHORT);
                        break;

                    case 1:
                        Toast.makeText(AsmrActivity.this,"沉浸时刻",Toast.LENGTH_SHORT);
                        break;
                    case 2:
                        Toast.makeText(AsmrActivity.this,"自由创作",Toast.LENGTH_SHORT);
                        break;
                    case 3:
                        Toast.makeText(AsmrActivity.this,"官方推荐",Toast.LENGTH_SHORT);
                        break;
                }
            }

            @Override //state的状态有三种，0为什么都没有做。1表示正在滑动，2滑动完毕
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}